package com.gitee.jiaojinxin.oauth2.auth.bo;

import com.gitee.jiaojinxin.oauth2.auth.dao.IMyClientDAO;
import com.gitee.jiaojinxin.oauth2.auth.model.MyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Service;

@Service
public class MyClientBOImpl implements ClientDetailsService {

    @Autowired
    private IMyClientDAO clientDAO;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        MyClient client = clientDAO.findOne(Example.of(MyClient.builder().clientId(clientId).build()))
                .orElseGet(MyClient::new);
        if (null == client.getId()) {
            throw new ClientRegistrationException("客户端不存在");
        }
        return client;
    }
}
